#include "ftp_client.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#include <pthread.h>

//ftp数据交互线程
void *ftp_thread_task(void *arg)
{
	int iRet = 0;
	char tmpBuf[512];
	unsigned char ip1,ip2,ip3,ip4,port1,port2;
	FTP_CLIENT_T *pFtpClient = (FTP_CLIENT_T *)arg;
	FTP_SERVER_INFO_T szFtpServer;

	FTP_SERVER_INFO_T stFtpServerInfo;
	memset(&stFtpServerInfo, 0, sizeof(stFtpServerInfo));

	snprintf(stFtpServerInfo.szUserName, sizeof(stFtpServerInfo.szUserName),"USER %s\r\n",FTP_USER_NAME);
	iRet = pFtpClient->pFunFtp_send(pFtpClient->iCtrlSock,stFtpServerInfo.szUserName,strlen(stFtpServerInfo.szUserName));
	printf("iRet:%d,%s\n",iRet,stFtpServerInfo.szUserName);
	iRet = pFtpClient->pFunFtp_recv(pFtpClient->iCtrlSock,tmpBuf, sizeof(tmpBuf));
	printf("iRet:%d,%s\n",iRet,tmpBuf);
	
	snprintf(stFtpServerInfo.szPassWd,sizeof(stFtpServerInfo.szPassWd),"PASS %s\r\n",FTP_PASSWD);
	iRet = pFtpClient->pFunFtp_send(pFtpClient->iCtrlSock,stFtpServerInfo.szPassWd,strlen(stFtpServerInfo.szPassWd));
	printf("iRet:%d,%s\n",iRet,stFtpServerInfo.szPassWd);
	iRet = pFtpClient->pFunFtp_recv(pFtpClient->iCtrlSock,tmpBuf, sizeof(tmpBuf));
	printf("iRet:%d,%s\n",iRet,tmpBuf);

	//至此就登录成功了:可以去实现其他的一些业务，比如说去下载和上传文件，修改目录、创建目录等等

	//
	snprintf(tmpBuf,sizeof(tmpBuf),"%s","PWD\r\n");
	iRet = pFtpClient->pFunFtp_send(pFtpClient->iCtrlSock,tmpBuf,strlen(tmpBuf));
	memset(tmpBuf, 0, sizeof(tmpBuf));
	iRet = pFtpClient->pFunFtp_recv(pFtpClient->iCtrlSock,tmpBuf, sizeof(tmpBuf));
	printf("iRet:%d,%s\n",iRet,tmpBuf);

	//将服务器设置成被动模式
	memset(tmpBuf, 0, sizeof(tmpBuf));
	snprintf(tmpBuf,sizeof(tmpBuf),"%s","PASV\r\n");
	iRet = pFtpClient->pFunFtp_send(pFtpClient->iCtrlSock,tmpBuf,strlen(tmpBuf));
	memset(tmpBuf, 0, sizeof(tmpBuf));
	iRet = pFtpClient->pFunFtp_recv(pFtpClient->iCtrlSock,tmpBuf, sizeof(tmpBuf));
	printf("iRet:%d,%s\n",iRet,tmpBuf);
	sscanf(strchr(tmpBuf,'(')+1,"%hhu,%hhu,%hhu,%hhu,%hhu,%hhu",&ip1,&ip2,&ip3,&ip4,&port1,&port2);
	snprintf(szFtpServer.szServerIP,sizeof(szFtpServer.szServerIP),"%hhu.%hhu.%hhu.%hhu",ip1,ip2,ip3,ip4);
	szFtpServer.serverPort = port1*256+port2;
	pFtpClient->iDataSock = pFtpClient->pFunFtp_connect(&szFtpServer);
	printf("sucess connect:%d\n",pFtpClient->iDataSock);

}

int main(int argc, char *argv[])
{
	int iRet = -1;
	char szRecv[MAX_RECV_BUF_SIZE] = {0};
	pthread_t threadFtpId;  //ftp线程id
	FTP_SERVER_INFO_T stFtpServer;
	
	/*1.获取ftp结构体*/
	FTP_CLIENT_T *pFtpClient = ftp_get_client_param();
	if(NULL == pFtpClient)
	{
		printf("ftp_get_client_param failed\n");
		return -1;
	}

	memset(&stFtpServer,0, sizeof(stFtpServer));
	
	snprintf(stFtpServer.szServerIP,sizeof(stFtpServer.szServerIP),"%s",FTP_SERVER_IP);
	stFtpServer.serverPort = FTP_SERVER_PORT;
	/*连接ftp服务器*/
	pFtpClient->iCtrlSock = pFtpClient->pFunFtp_connect(&stFtpServer);
	if(pFtpClient->iCtrlSock < 0)
	{
		printf("pFunFtp_connect failed\n");
		return -1;
	}
	printf("pFtpClient->iCtrlSock:%d\n",pFtpClient->iCtrlSock);

	
	/*从服务器获取数据*/
	iRet = pFtpClient->pFunFtp_recv(pFtpClient->iCtrlSock,szRecv, sizeof(szRecv));
	if(iRet < 0)
	{
		printf("pFunFtp_recv failed\n");
		return -1;
	}

	printf("szRecv:%s\n",szRecv);

	//如果服务器应答就绪，创建数据处理线程去处理和服务器交互
	pthread_create(&pFtpClient->threadFtpID, NULL,ftp_thread_task,(void*)pFtpClient);

	while(1);
	
	return 0;
}




















